package icselab.dbaccess;

import icselab.util.ConfigParser;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DeployDAO {
	
    private static final String DOWNLOADING = "downloading";
    private static final String UNDEPLOYED = "undeployed";
    private static final String DONE = "done";
	private String connStr = null;
	private String username = null;
	private String password = null;
	private Connection conn = null;
	
	public DeployDAO(){
		ConfigParser parser = new ConfigParser();
		connStr = parser.getElement("connectionstring");
		username = parser.getElement("username");
		password = parser.getElement("password");

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(connStr, username, password);
			System.out.println("get the connection of "+ connStr);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * insert instance into table deploy_info
	 */
	public void insertDeployInfo(String instanceID, String deployDir){		
		//access database to insert deployment info
		try{		
			System.out.println("Insert the instance_info of "+ instanceID +" into database");
			PreparedStatement stmt = conn.prepareStatement("INSERT INTO deploy_info(InstanceId, InstanceName, InstanceDir, InstanceUrl, DeployStatus) VALUES (?,?,?,?,?)");
			stmt.setString(1, instanceID);
			stmt.setString(2, instanceID+".war");
			stmt.setString(3, deployDir);
			stmt.setString(4, "file:/"+deployDir);
			stmt.setString(5, UNDEPLOYED);
			stmt.execute();
		}catch (Exception ex){
			ex.printStackTrace();
		} 
	}
	
	/*
	 * insert instance into table deploy_info
	 */
	public void updateDeployInfo(String instanceID, String deployStatus){		
		//access database to insert deployment info
		try{
			System.out.println("update the instance_info of "+ instanceID +" into database");
			PreparedStatement stmt = conn.prepareStatement("UPDATE deploy_info SET DeployStatus = ? WHERE InstanceID = ? ");
			stmt.setString(1, deployStatus);
			stmt.setString(2, instanceID);
			stmt.execute();
		}catch (Exception ex){
			ex.printStackTrace();
		} 
	}
	
	/*
	 * insert instance into table deploy_info
	 */
	public void deleteDeployInfo(String instanceID){		
		//access database to insert deployment info
		try{
			PreparedStatement stmt = conn.prepareStatement("DELETE FROM deploy_info WHERE InstanceID = ? ");
			stmt.setString(1, instanceID);
			stmt.execute();
		}catch (Exception ex){
			ex.printStackTrace();
		} 
	}
	
	/*
	 * get instance URL by instance id
	 */
	public String getUrlByInstanceID(String instanceID){
		try {
			PreparedStatement stmt = conn.prepareStatement("select InstanceUrl from deploy_info where InstanceId = ?");
			stmt.setString(1, instanceID);
			String iUrl = "";
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) 
				iUrl = rs.getString("InstanceUrl");
			return iUrl;
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		/*finally{
		try{
			conn.close();
		}catch (Exception ex2){
			ex2.printStackTrace();
		}
		}*/
		return "";
	}
	
	/*
	 * get instance info by instance id
	 */
	public ResultSet getInstanceByID(String instanceID){
		try {
			CallableStatement statement = conn.prepareCall("{call sp_getInstanceByID(?)}");	
			statement.setString(1, instanceID);
			ResultSet rs =statement.executeQuery();
			return rs;
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		/*finally{
		try{
			conn.close();
		}catch (Exception ex2){
			ex2.printStackTrace();
		}
		}*/
		return null;
	}
	
	/*
	 * get running instances by status
	 */
	public ResultSet getRunningInstance(){
		try {
			CallableStatement statement = conn.prepareCall("{call sp_getInstanceByStatus(?)}");	
			statement.setString(1, DONE);
			ResultSet rs =statement.executeQuery();
			System.out.println("get running instances");
			return rs;
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return null;
	}
	
	/*
	 * get instance path by instance id
	 */
	public String getDirByInstanceID(String instanceID){		
		try {
			PreparedStatement stmt = conn.prepareStatement("select InstanceDir from deploy_info where InstanceId = ?");
			stmt.setString(1, instanceID);
			String iDir = "";
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) 
				iDir = rs.getString("InstanceDir");
			return iDir;
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		/*finally{
		try{
			conn.close();
		}catch (Exception ex2){
			ex2.printStackTrace();
		}
		}*/
		return "";
	}
}
